/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mailbox.FolderNode;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.OperationContextData;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetFolder
/*     */   extends MailDocumentHandler
/*     */ {
/*  46 */   private static final String[] TARGET_FOLDER_PATH = { "folder", "l" };
/*  47 */   private static final String[] RESPONSE_ITEM_PATH = { "folder" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  51 */     return TARGET_FOLDER_PATH;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  56 */     return false;
/*     */   }
/*     */   
/*     */   protected String[] getResponseItemPath()
/*     */   {
/*  61 */     return RESPONSE_ITEM_PATH;
/*     */   }
/*     */   
/*  64 */   static final String DEFAULT_FOLDER_ID = Integer.toString(1);
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  68 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  69 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  70 */     OperationContext octxt = getOperationContext(zsc, context);
/*  71 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*     */ 
/*  74 */     Element eFolder = request.getOptionalElement("folder");
/*  75 */     ItemId iid; if (eFolder != null) {
/*  76 */       String uuid = eFolder.getAttribute("uuid", null);
/*  77 */       ItemId iid; ItemId iid; if (uuid != null) {
/*  78 */         Folder folder = mbox.getFolderByUuid(octxt, uuid);
/*  79 */         iid = new ItemId(folder);
/*     */       } else {
/*  81 */         iid = new ItemId(eFolder.getAttribute("l", DEFAULT_FOLDER_ID), zsc);
/*     */       }
/*     */       
/*  84 */       String path = eFolder.getAttribute("path", null);
/*  85 */       if (path != null) {
/*  86 */         Pair<Folder, String> resolved = mbox.getFolderByPathLongestMatch(octxt, iid.getId(), path);
/*  87 */         Folder folder = (Folder)resolved.getFirst();
/*  88 */         String overflow = (String)resolved.getSecond();
/*     */         
/*  90 */         if (overflow == null) {
/*  91 */           iid = new ItemId(folder);
/*  92 */         } else { if ((folder instanceof Mountpoint))
/*     */           {
/*  94 */             ItemId iidTarget = ((Mountpoint)folder).getTarget();
/*  95 */             eFolder.addAttribute("l", iidTarget.toString()).addAttribute("path", overflow);
/*  96 */             return proxyRequest(request, context, new ItemId(folder), iidTarget);
/*     */           }
/*  98 */           throw MailServiceException.NO_SUCH_FOLDER(path);
/*     */         }
/*     */       }
/*     */     } else {
/* 102 */       iid = new ItemId(DEFAULT_FOLDER_ID, zsc);
/*     */     }
/*     */     
/* 105 */     int depth = (int)request.getAttributeLong("depth", -1L);
/* 106 */     boolean traverse = request.getAttributeBool("tr", false);
/* 107 */     boolean visible = request.getAttributeBool("visible", false);
/* 108 */     boolean needGranteeName = request.getAttributeBool("needGranteeName", true);
/*     */     
/* 110 */     String v = request.getAttribute("view", null);
/* 111 */     MailItem.Type view = v == null ? null : MailItem.Type.of(v);
/*     */     
/* 113 */     Mailbox.FolderNode rootnode = filterByView(mbox.getFolderTree(octxt, iid, visible), view);
/*     */     
/* 115 */     Element response = zsc.createElement(MailConstants.GET_FOLDER_RESPONSE);
/* 116 */     if (rootnode != null) {
/* 117 */       if (needGranteeName) {
/* 118 */         OperationContextData.addGranteeNames(octxt, rootnode);
/*     */       } else {
/* 120 */         OperationContextData.setNeedGranteeName(octxt, false);
/*     */       }
/*     */       
/* 123 */       List<ExpandableMountpoint> mounts = Lists.newArrayList();
/* 124 */       Element folderRoot = encodeFolderNode(rootnode, response, ifmt, octxt, true, depth, view, traverse, mounts);
/*     */       
/* 126 */       if ((rootnode.mFolder != null) && ((rootnode.mFolder instanceof Mountpoint))) {
/* 127 */         mounts.add(new ExpandableMountpoint(folderRoot, (Mountpoint)rootnode.mFolder, depth));
/*     */       }
/* 129 */       for (ExpandableMountpoint empt : mounts) {
/* 130 */         expandMountpoint(empt, request, context);
/*     */       }
/*     */     }
/*     */     
/* 134 */     return response;
/*     */   }
/*     */   
/*     */   public static Element encodeFolderNode(Mailbox.FolderNode node, Element parent, ItemIdFormatter ifmt, OperationContext octxt) throws ServiceException
/*     */   {
/* 139 */     return encodeFolderNode(node, parent, ifmt, octxt, false, -1, null, false, null);
/*     */   }
/*     */   
/*     */ 
/*     */   private static Element encodeFolderNode(Mailbox.FolderNode node, Element parent, ItemIdFormatter ifmt, OperationContext octxt, boolean exposeAclAccessKey, int depth, MailItem.Type view, boolean traverse, List<ExpandableMountpoint> mounts)
/*     */     throws ServiceException
/*     */   {
/* 146 */     Folder folder = node.mFolder;
/* 147 */     Element eFolder; if (folder != null) {
/* 148 */       Element eFolder = ToXML.encodeFolder(parent, ifmt, octxt, folder, -4194305, exposeAclAccessKey);
/*     */       
/* 150 */       if ((traverse) && (mounts != null) && ((folder instanceof Mountpoint)) && (folder.getMailbox().hasFullAccess(octxt))) {
/* 151 */         mounts.add(new ExpandableMountpoint(eFolder, (Mountpoint)folder, depth));
/*     */       }
/*     */     } else {
/* 154 */       eFolder = parent.addElement("folder").addAttribute("id", ifmt.formatItemId(node.mId)).addAttribute("name", node.mName);
/*     */     }
/*     */     
/* 157 */     if (depth == 0) {
/* 158 */       return eFolder;
/*     */     }
/*     */     
/* 161 */     int remainingDepth = depth > 0 ? depth - 1 : depth;
/* 162 */     for (Mailbox.FolderNode subNode : node.mSubfolders) {
/* 163 */       encodeFolderNode(subNode, eFolder, ifmt, octxt, exposeAclAccessKey, remainingDepth, view, traverse, mounts);
/*     */     }
/*     */     
/* 166 */     return eFolder;
/*     */   }
/*     */   
/*     */   private static class ExpandableMountpoint {
/*     */     final Element elt;
/*     */     final Mountpoint mpt;
/*     */     final int depth;
/*     */     
/*     */     ExpandableMountpoint(Element elt, Mountpoint mpt, int depth) {
/* 175 */       this.elt = elt;this.mpt = mpt;this.depth = depth;
/*     */     }
/*     */     
/*     */     public String toString()
/*     */     {
/* 180 */       return this.mpt.getName() + " -> " + this.mpt.getTarget() + (this.depth >= 0 ? " [depth " + this.depth + "] " : "");
/*     */     }
/*     */   }
/*     */   
/*     */   private void expandMountpoint(ExpandableMountpoint empt, Element origRequest, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/* 187 */     Element request = origRequest.clone();
/*     */     
/* 189 */     if (empt.depth >= 0) {
/* 190 */       request.addAttribute("depth", empt.depth);
/*     */     }
/*     */     
/* 193 */     request.addAttribute("tr", false);
/*     */     
/*     */ 
/* 196 */     Element eFolder = request.getOptionalElement("folder");
/* 197 */     if (eFolder != null) {
/* 198 */       eFolder.detach();
/*     */     }
/* 200 */     ItemId iidRemote = empt.mpt.getTarget();
/* 201 */     request.addElement("folder").addAttribute("l", iidRemote.toString());
/*     */     try
/*     */     {
/* 204 */       Element proxied = proxyRequest(request, context, new ItemId(empt.mpt), iidRemote);
/*     */       
/* 206 */       proxied = proxied.getOptionalElement("folder");
/* 207 */       if (proxied != null) {
/* 208 */         ToXML.transferMountpointContents(empt.elt, proxied);
/*     */       }
/*     */     } catch (ServiceException se) {
/* 211 */       empt.elt.addAttribute("broken", true);
/*     */     }
/*     */   }
/*     */   
/*     */   private Mailbox.FolderNode filterByView(Mailbox.FolderNode node, MailItem.Type viewFilter) {
/* 216 */     if ((viewFilter == null) || (viewFilter == MailItem.Type.UNKNOWN)) {
/* 217 */       return node;
/*     */     }
/*     */     
/*     */     Iterator<Mailbox.FolderNode> it;
/* 221 */     if (!node.mSubfolders.isEmpty()) {
/* 222 */       for (it = node.mSubfolders.iterator(); it.hasNext();) {
/* 223 */         if (filterByView((Mailbox.FolderNode)it.next(), viewFilter) == null) {
/* 224 */           it.remove();
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 230 */     if ((node.mFolder != null) && (node.mId != 3) && (node.mFolder.getDefaultView() != viewFilter)) {
/* 231 */       node.mFolder = null;
/*     */     }
/*     */     
/*     */ 
/* 235 */     return (node.mFolder == null) && (node.mSubfolders.isEmpty()) ? null : node;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/GetFolder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */